#!/bin/bash
#判断用户组及用户是否存在,不存在创建
#初始化远程终端时,将该脚本上传到远程终端root目录下,运行完成后删除

error_exit(){
	echo "$1"
	exit 1
}

group=$1
user=$2


#判断用户是否存在
isUser=`id -nu ${user} 2> /dev/null`
isGroup=`egrep "^${group}" /etc/group`
if [ ! ${isGroup} ]
then
    groupadd ${group} || error_exit "创建用户组${group}失败"
fi

if [ ! ${isUser} ]
then
    useradd -s /bin/bash -g ${group} -d /home/${user} -m ${user} || error_exit "创建用户${user}失败"
#    useradd -s /bin/bash -g beetle -d /home/beetle -m beetle
    usermod -L ${user} || error_exit "创建用户,锁定${user}失败"
else
#    usermod -G ${group} ${user}
    userdel -r ${user}
fi
